package com.glory.brepda.activity.lanjian;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.FrameGetTime;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class WaferReceiveOKActivity extends BaseActivity {

    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.etWoID)
    FreeUI_ClearEditText etWoID;
    @BindView(R.id.rb_cq)
    RadioButton rbCq;
    @BindView(R.id.rb_no)
    RadioButton rb_no;
    @BindView(R.id.lly01)
    LinearLayout lly01;
    @BindView(R.id.lly02)
    LinearLayout lly02;
    @BindView(R.id.eth)
    FreeUI_ClearEditText eth;
    @BindView(R.id.etNan)
    FreeUI_ClearEditText etNan;
    @BindView(R.id.etH)
    FreeUI_ClearEditText etH;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_wafer_receive_okactivity;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setVisibility(View.GONE);
        lly01.setVisibility(View.GONE);
        lly02.setVisibility(View.GONE);
        setMainLayout(llMain);

        initEdit(etWoID, s -> {

            FreeUI_EntityCallBack<BaseResponseBean<FrameGetTime>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<FrameGetTime>>
                    ("正在查询领料信息", new TypeToken<BaseResponseBean<FrameGetTime>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<FrameGetTime>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplication(), true, getSupportFragmentManager())) {
                        FrameGetTime fgt = response.body().getResponse().getBody();
                        // 当前时间使用pda可能会存在有误的情况
                        String fst =  fgt.getDATEJG()+" 小时";
                        etH.setText(fst);
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<FrameGetTime>> response) {
                    super.onError(response);
                    loadError(response.getException(), "QUERY");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTION", "QUERY");
            map.addProperty("WO_ID",getText(etWoID).trim());
            NetUtils.requestPostNet("IF.FRAME_TAKE", "Common", getApplication(), map, callBack);
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.rb_no,R.id.rb_cq,R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_no:
                lly01.setVisibility(View.GONE);
                lly02.setVisibility(View.GONE);
                break;
            case R.id.rb_cq:
                lly01.setVisibility(View.VISIBLE);
                lly02.setVisibility(View.VISIBLE);
                break;
            case R.id.btn:
                submit();
                break;
        }
    }

    public void submit(){
        if(getText(etWoID).equals("") || getText(etH).equals("")){
            showToast("请完善信息!",true);
            return;
        }
        if( rb_no.isChecked() == false && rbCq.isChecked() == false){
            showToast("请选择是否超期!",true);
            return;
        }
        if(rbCq.isChecked()){
            if(getText(eth).equals("") || getText(etNan).equals("")){
                showToast("请填写超期数据!",true);
                return;
            }

            double db = Double.parseDouble(getText(etH).split(" ")[0]);
            double bz = Double.parseDouble(getText(eth));
            if(bz >= db){
                showToast("超期必须时间间隔大于最大间隔时间!",true);
                return;
            }
        }

        // submit
        FreeUI_EntityCallBack<BaseResponseBean<FrameGetTime>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<FrameGetTime>>
                ("正在查询领料信息", new TypeToken<BaseResponseBean<FrameGetTime>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<FrameGetTime>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplication(), true, getSupportFragmentManager())) {
//                    showConfirmDialog(WaferReceiveOKActivity.this, "领取成功!!!");
                    showToast("领取成功!",true);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<FrameGetTime>> response) {
                super.onError(response);
                loadError(response.getException(), "QUERY");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTION", "SURE");
        map.addProperty("WO_ID",getText(etWoID).trim());
        map.addProperty("OK_MAN", Constant.CURRENT_DESC);
        map.addProperty("IS_TIME_OUT", rbCq.isChecked()?"是":"否");
        map.addProperty("TIME_JG", getText(etH).split(" ")[0]);
        map.addProperty("TIME_OUT_MAN",getText(etNan));
        map.addProperty("STANDARD_H",getText(eth));
        NetUtils.requestPostNet("IF.FRAME_TAKE", "Common", getApplication(), map, callBack);

    }
}